package com.medi.chats.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * Date公共类
 */
public class DateUtil {
    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);//log日志
    /**
     * @param
     * @Description:
     * @Author: jww
     * @CreateDate: 2017/12/1 15:19
     */
    public static Date getDate() {
        Date date = new Date();
        return date;
    }

    /**
     * 日期转化 Date to String
     *
     * @param date   Date
     * @param format String
     * @return
     */
    public static String dateToString(Date date, String format) {
        DateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    /**
     * 日期转化 String To Date
     *
     * @param dateStr String
     * @param format  String
     * @return
     */
    public static Date stringToDate(String dateStr, String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            return df.parse(dateStr);
        } catch (ParseException e) {
            logger.error(e.toString());
            return null;
        }
    }

    /**
     * 日期转化 LocalDate To Date
     *
     * @param localDate LocalDate
     * @return
     */
    public Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 日期转化 LocalDateTime To Date
     *
     * @param localDateTime LocalDateTime
     * @return
     */
    public Date localDateToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 日期转化  Date To LocalDateTime
     *
     * @param date Date
     * @return
     */
    public LocalDateTime dateToLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * 日期转化  Date To LocalDate
     *
     * @param date Date
     * @return
     */
    public LocalDate dateToLocalDate(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 把long 转换成 日期 再转换成String类型
     */
    public String longToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }
    /**
     * 日期的加减运算
     */
    public static Date calcDate(Date date, int year,int month,int day, int hour,int minute,int second) {
        Calendar gc = Calendar.getInstance();
        gc.setTime(date);
        gc.add(Calendar.YEAR, year);
        gc.add(Calendar.MONTH, month);
        gc.add(Calendar.DAY_OF_MONTH, day);
        gc.add(Calendar.HOUR_OF_DAY, hour);
        gc.add(Calendar.MINUTE, minute);
        gc.add(Calendar.SECOND, second);
        gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DAY_OF_MONTH), gc.get(Calendar.HOUR_OF_DAY), gc.get(Calendar.MINUTE), gc.get(Calendar.SECOND));
        return gc.getTime();
    }
    /**
     * 验证是否为日期格式
     */
    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 设置lenient为false.
            // 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }
}
